package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1.pcl;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.device.dao.db.DeviceType;
import com.gzhryc.shared_device.common.device.services.DeviceTypeService;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.ELoginUserRoleType;
import com.gzhryc.shared_device.oem.h5.agent.LoginUser;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.pcl.PCLFeeTemplateListRequest;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.pcl.PCLFeeTemplateListResponse;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.pcl.PCLPlaceCostConfig;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.pcl.PCLPlaceCostConfigListResponse;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.place.PlaceBusinessConfigListRequest;
import com.gzhryc.shared_device.pcl.PCLSetMealCostContent;
import com.gzhryc.shared_device.pcl.code.dao.db.PCLFeeTemplate;
import com.gzhryc.shared_device.pcl.code.services.PCLFeeTemplateService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class PCLFeeTemplateController {

    @Route(value = "/agent/api/v1/place/business_config/pcl/list.data")
    public PCLPlaceCostConfigListResponse deviceTypeList(HttpServletRequest request){
        PCLPlaceCostConfigListResponse apiResp = new PCLPlaceCostConfigListResponse(DtoConstants.NULL_DATA,"暂无数据");
        PlaceBusinessConfigListRequest apiReq = WebUtils.getJsonRequest(request, PlaceBusinessConfigListRequest.class);
        if (apiReq != null && apiReq.getBody().getPlaceId() != null) {
            apiResp.setCode(DtoConstants.SUCCESS,null);
            apiResp.getBody().setItems(new ArrayList<>());

            PlaceBusinessConfig businessConfig = PlaceBusinessConfigService.self().getByKeys(apiReq.getBody().getPlaceId(), EBusinessType.PCL.index());
            if (businessConfig != null && StringTools.isNotBlank(businessConfig.getCostContent())) {
                PCLSetMealCostContent costContent = JsonTools.fromJson(businessConfig.getCostContent(), PCLSetMealCostContent.class);
                if (costContent != null && costContent.getItems() != null && costContent.getItems().size() > 0) {
                    List<String> deviceTypeIds = new ArrayList<>();
                    List<Long> feeTemplateIds = new ArrayList<>();
                    for (PCLSetMealCostContent.Item item : costContent.getItems()) {
                        deviceTypeIds.add(item.getDeviceTypeId());
                        feeTemplateIds.add(item.getFeeTemplateId());
                    }

                    List<DeviceType> deviceTypeList = DeviceTypeService.self().findByIds(deviceTypeIds);
                    List<PCLFeeTemplate> feeTemplateList = PCLFeeTemplateService.self().findJsonByIds(feeTemplateIds);

                    for (PCLSetMealCostContent.Item item : costContent.getItems()) {
                        PCLPlaceCostConfig.Item temp = new PCLPlaceCostConfig.Item();
                        temp.setDeviceTypeId(item.getDeviceTypeId());
                        temp.setFeeTemplateId(item.getFeeTemplateId());

                        if (deviceTypeList != null && deviceTypeList.size() > 0) {
                            for (DeviceType deviceType : deviceTypeList) {
                                if (deviceType.getId().equals(temp.getDeviceTypeId())) {
                                    temp.setDeviceTypeName(deviceType.getName());
                                    break;
                                }
                            }
                        }
                        if (feeTemplateList != null && feeTemplateList.size() > 0) {
                            for (PCLFeeTemplate feeTemplate : feeTemplateList) {
                                if (feeTemplate.getId().equals(temp.getFeeTemplateId())) {
                                    temp.setFeeTemplateName(feeTemplate.getTitle());
                                    break;
                                }
                            }
                        }
                        apiResp.getBody().getItems().add(temp);
                    }
                }
            }

            List<PCLFeeTemplate> feeTemplateList = PCLFeeTemplateService.self().findJoinDefPlatform();
            if (feeTemplateList != null && feeTemplateList.size() > 0) {
                for (PCLFeeTemplate feeTemplate : feeTemplateList) {
                    boolean flag = true;
                    for (PCLPlaceCostConfig.Item item : apiResp.getBody().getItems()) {
                        if (item.getDeviceTypeId().equals(feeTemplate.getDeviceTypeId())) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) {
                        PCLPlaceCostConfig.Item item = new PCLPlaceCostConfig.Item();
                        item.setDeviceTypeId(feeTemplate.getDeviceTypeId());
                        item.setDeviceTypeName(feeTemplate.getDeviceTypeName());
                        item.setFeeTemplateId(feeTemplate.getId());
                        item.setFeeTemplateName(feeTemplate.getTitle());
                        apiResp.getBody().getItems().add(item);
                    }
                }
            }
        }
        return apiResp;
    }

    @Route(value = "/agent/api/v1/pcl/fee_template/list.data")
    public PCLFeeTemplateListResponse list(HttpServletRequest request) {
        PCLFeeTemplateListResponse apiResp = new PCLFeeTemplateListResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
        PCLFeeTemplateListRequest apiReq = WebUtils.getJsonRequest(request, PCLFeeTemplateListRequest.class);
        LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
        if (loginUser != null) {
            List<PCLFeeTemplate> feeTemplateList = null;
            if(ELoginUserRoleType.Agent.index().equals(loginUser.getRoleType())){
                if(StringTools.isNotBlank(apiReq.getBody().getDeviceTypeId())) {
                    feeTemplateList = PCLFeeTemplateService.self().findByUserIdAndDeviceTypeId(loginUser.getId(),apiReq.getBody().getDeviceTypeId());
                }else{
                    feeTemplateList = PCLFeeTemplateService.self().findByUserId(loginUser.getId());
                }
            }else if(ELoginUserRoleType.Partner.index().equals(loginUser.getRoleType())){
                if(StringTools.isNotBlank(apiReq.getBody().getDeviceTypeId())) {
                    feeTemplateList = PCLFeeTemplateService.self().findByUserIdAndDeviceTypeId(loginUser.getParentId(),apiReq.getBody().getDeviceTypeId());
                }else{
                    feeTemplateList = PCLFeeTemplateService.self().findByUserId(loginUser.getParentId());
                }
            }

            apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
            apiResp.getBody().setItems(new ArrayList<>());

            //获取全局
            if(StringTools.isNotBlank(apiReq.getBody().getDeviceTypeId())) {
                PCLFeeTemplate feeTemplate = PCLFeeTemplateService.self().getDefPlatformByDeviceTypeId(apiReq.getBody().getDeviceTypeId());
                if(feeTemplate != null) {
                    PCLFeeTemplateListResponse.Item item = new PCLFeeTemplateListResponse.Item();
                    item.setId(feeTemplate.getId());
                    item.setTitle(feeTemplate.getTitle());
                    item.setFeeInfo(feeTemplate.getFeeInfo());
                    apiResp.getBody().getItems().add(item);
                }
            }

            if(feeTemplateList != null && feeTemplateList.size() > 0){
                for(PCLFeeTemplate feeTemplate : feeTemplateList){
                    PCLFeeTemplateListResponse.Item item = new PCLFeeTemplateListResponse.Item();
                    item.setId(feeTemplate.getId());
                    item.setTitle(feeTemplate.getTitle());
                    item.setFeeInfo(feeTemplate.getFeeInfo());
                    apiResp.getBody().getItems().add(item);
                }
            }
        }
        return apiResp;
    }
}
